<template>
    <n-flex justify="center">
        <n-form label-placement="left" :label-width="160">
            <FormItemComponent title="回测场景：" tips="设置初始资金">
                <n-select class="w-96" v-model:value="baseinfo_form.scenario_id" :options="options.scenario_list"  @update:value="handleUpdateValue"
                    placeholder="请选择回测的场景" />
            </FormItemComponent>
            <FormItemComponent title="回测类型：" tips="设置初始资金">
                <n-select class="w-96" v-model:value="baseinfo_form.backtest_type" :options="options.backtest_type"
                    placeholder="请选择回测的类别" />
            </FormItemComponent>
            <FormItemComponent title="关注指标：" tips="设置初始资金">
                <n-select class="w-96" v-model:value="baseinfo_form.indicator_type"
                    :options="options.indicator_type_list" placeholder="请选择回测的类别" />
            </FormItemComponent>
            <FormItemComponent v-if="baseinfo_form.indicator_type == 'custom'" title="请选择指标：" tips="设置初始资金">
                <n-select class="w-96" multiple placeholder="请选择最多八个指标" @update:value="checkIndicatorListLength"
                    v-model:value="baseinfo_form.custom_indicator_list" :options="options.custom_indicator_list"
                    :render-option="renderOption" clearable/>
            </FormItemComponent>
            <FormItemComponent title="比照标的：" tips="设置初始资金">
                <n-select class="w-96" v-model:value="baseinfo_form.compare_stock_id"
                    :options="options.compare_stock_list" placeholder="请选择回测的比照标的" />
            </FormItemComponent>

            <FormItemComponent title="初始投资金额：" tips="设置初始资金">
                <n-input-number v-model:value="baseinfo_form.cash" class="w-96" placeholder="请输入初始金额，单位：元" clearable
                    :precision="2">
                    <template #suffix>
                        元
                    </template>
                </n-input-number>
            </FormItemComponent>


        </n-form>
    </n-flex>
</template>

<script setup>
import FormItemComponent from '@/components/FormItemComponent.vue';
import { ref, onMounted, h } from 'vue';
import { useScenarioStore } from '@/stores/scenarioStore';
import { useStockStore } from '@/stores/stockStore';
import { NTooltip,useMessage } from "naive-ui";
const message = useMessage();
const scenarioStore = useScenarioStore();
const stockStore = useStockStore();
const props = defineProps(["baseinfo_form"]);

const options = ref({
    scenario_list: [],
    backtest_type: [],
    indicator_type_list: [],
    custom_indicator_list: [],
    compare_stock_list: []
});

const renderOption = ({ node, option }) => h(NTooltip, null, {
    trigger: () => node,
    default: () => option.tips
});
const checkIndicatorListLength = (value) => {
    if (value.length > 8) {
        value.splice(8, value.length - 8);
        message.info("至多选择 -8- 个指标");
    }
}
const handleUpdateValue = (value) => {
    scenarioStore.getScenarioById(value);
}
const setOptions = () => {
    let temp_scenario_list = JSON.parse(JSON.stringify(scenarioStore.scenario_list));
    for (let i of temp_scenario_list) {
        options.value.scenario_list.push({
            label: i.name,
            value: i.id
        })
    }
    options.value.backtest_type.push({
        label: '个股回测',
        value: 'stock'
    }, {
        label: '组合回测',
        value: 'portfolio'
    })
    options.value.indicator_type_list.push({
        label: '投资风险与回报',
        value: 'type1'
    }, {
        label: '投资稳定性与波动处理',
        value: 'type2'
    }, {
        label: '自定义',
        value: 'custom'
    })
    options.value.custom_indicator_list.push({
        label: '日均回报率',
        tips: '收益 - 平均每日的回报率',
        value: 'daily_return_rate'
    },
    {
        label: '年化回报率',
        tips: '收益 - 预计每年的回报率',
        value: 'returns_of_annual'
    },{
        label: '资产变化标准差',
        tips: '风险 - 资产金额变化的标准差',
        value: 'variance_of_assets'
    },{
        label: '回报率变化的标准差',
        tips: '风险 - 回报率变化的标准差',
        value: 'variance_of_returns'
    },{
        label: '策略复杂度',
        tips: '策略 - 该策略的复杂度',
        value: 'complexity_of_strategy'
    },{
        label: '最大回撤持续时间',
        tips: '策略 - 该策略在发生最大回撤时，最大回撤的持续时间',
        value: 'max_drawdown_duration'
    },{
        label: '最大回撤',
        tips: '风险 - 该策略持续期间发生的最大回撤',
        value: 'max_drawdown'
    },
    {
        label: '夏普比率',
        tips: '风险与收益 - 每单位波动下能带来多少收益',
        value: 'sharpe_ratio'
    },
    {
        label: '所提诺比率',
        tips: '风险与收益 - 每单位风险下能带来多少收益',
        value: 'sortino_ratio'
    },
    {
        label: '欧米茄比率',
        tips: '收益 - 与市场的收益的关系',
        value: 'omega_ratio'
    },
);
    options.value.compare_stock_list.splice(0, options.value.comapre_stock_list);
    for (let i of stockStore.compare_stock_list) {
        options.value.compare_stock_list.push({
            label: i.name,
            value: i.id
        })
    }



}

onMounted(async () => {
    await scenarioStore.getScenarioList();
    await stockStore.getCompareStockList();
    setOptions();
})


</script>